package com.kmist.xiakexing.tools;


import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

/**
 * 数据库的在增删改查
 */
public class Dao {

    private static final String TAG = "DAO";
    public final DataBaseHelper helper;
    private String value;
    public SQLiteDatabase db;

    public Dao(Context context){
        Log.d(TAG, "====new");
        helper = new DataBaseHelper(context);
        db = helper.getWritableDatabase();
    }

    public void insert(){
        SQLiteDatabase db = helper.getWritableDatabase();
        //String sql = "insert into " + Constants.TABLE_NAME + "(_id,name,age,salary) values(?,?,?,?)";
        //db.execSQL(sql,new Object[]{1,"kk",10,100});

        //ContentValues values = new ContentValues();
        //values.put("_id", 2);
        //values.put("name", "aaa");
        //values.put("age", 2);
        //values.put("salary", 2);
        //db.insert(Constants.TABLE_NAME,null, values);
        //db.close();
    }

    public void delete(){
        //SQLiteDatabase db = helper.getWritableDatabase();
        //String sql = "delete from " + Constants.TABLE_NAME + " where age = 60";
        //db.execSQL(sql);
        //db.close();
    }

    public void update(){

        //SQLiteDatabase db = helper.getWritableDatabase();
        //String sql = "update " + Constants.TABLE_NAME + " set salary = 2 where age = 60";

        //数据库事务
        //db.beginTransaction();
        //try{
        //    db.execSQL(sql);
        //    db.setTransactionSuccessful();
        //} catch (Exception e) {
        //    e.printStackTrace();
        //}finally {
        //    db.close();
        //    db.endTransaction();
        //}
    }

    public String select(String table, String name){
        String result = null;
        Cursor c = db.query(table, null, "name=?", new String[]{name}, null, null, null);
        if (c.moveToFirst()) {
            do {
                int id = c.getInt(c.getColumnIndex("_id"));
                String mname = c.getString(c.getColumnIndex("name"));
                String zname = c.getString(c.getColumnIndex("zname"));
                String maindesc = c.getString(c.getColumnIndex("maindesc"));
                result = "\t\t\t\t"+maindesc;
                Log.d(TAG, "id=" + id + ",姓名=" + mname + ",年龄=" + zname);
            } while (c.moveToNext());
        }
        c.close();
        db.close();
        return result;
    }

//    public void getMapinfo(String table, String name, Map map){
//        db = helper.getReadableDatabase();
//        Cursor c = db.query(table, null, "name=?", new String[]{name}, null, null,null);
//        if(c.moveToFirst()){
//            do{
//
//            }while (c.moveToNext());
//        }
//    }

    public String getItemsChineseName(String pItemName){
        db = helper.getReadableDatabase();
        Cursor c = db.query("item", null, "pname=?", new String[]{pItemName}, null, null,null);
        if(c.moveToFirst()){
            do{
                return  c.getString(c.getColumnIndex("name"));
            }while (c.moveToNext());
        }
        return "";
    }
}
